using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Deepcode.Flux.Core.Systems.Workflow;
using Deepcode.Flux.Core.Systems.Workflow.Bases;
using Deepcode.Flux.Core.Systems.Workflow.Info;
using Deepcode.Flux.Core.Services.State;
using Deepcode.Flux.Core.Extensions;
using Deepcode.Flux.Core.Systems.Workflow.Interfaces;
using Deepcode.Flux.Core.Systems.Audit.Quick;

public partial class Private_Config_Workflow_ui_startupactionprop : Deepcode.Flux.Core.UI.AdministrationModulePage
{
	#region Permission Code Override
	public override string RequiredPermissionCode { get { return "FluxWorkflowEdit"; } }
	#endregion
	#region ViewState storage
	/// <summary>
	/// Stores the ID of the handler which this form
	/// is adding a new definition to.
	/// </summary>
	public string HandlerID
	{
		get
		{
			object o = ViewState["HandlerID"];
			if (o == null) return String.Empty;
			return (string)o;
		}
		set
		{
			ViewState["HandlerID"] = value;
		}
	}
	/// <summary>
	/// Stores the ID of the definition being edited
	/// </summary>
	public int DefinitionID
	{
		get
		{
			object o = ViewState["DefinitionID"];
			if (o == null) return -1;
			return (int)o;
		}
		set
		{
			ViewState["DefinitionID"] = value;
		}
	}
	/// <summary>
	/// Stores the ID of the process being edited
	/// </summary>
	public int ProcessID
	{
		get
		{
			object o = ViewState["ProcessID"];
			if (o == null) return -1;
			return (int)o;
		}
		set
		{
			ViewState["ProcessID"] = value;
		}
	}
	/// <summary>
	/// Stores the ID of the process start action being edited
	/// </summary>
	public int ProcessStartID
	{
		get
		{
			object o = ViewState["ProcessStartID"];
			if( o == null ) return -1;
			return (int) o;
		}
		set
		{
			ViewState["ProcessStartID"] = value;
		}
	}
	#endregion

	private IWorkflowHandlerMethodConfig ConfigClass = null;

	#region Page Load Event
	/// <summary>
	/// Page Load event
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!Page.IsPostBack)
		{
			// Store any state information from the calling listing
			this.HandlerID = GetAttribute("HandlerID", "");
			this.DefinitionID = GetAttribute("DefinitionID", -1);
			this.ProcessID = GetAttribute("ProcessID", -1);
			this.ProcessStartID = GetAttribute("ProcessStartID", -1);
			// Load info 
			Handler h = new Handlers().GetHandlerByID( this.HandlerID );
			if( h == null ) GoBack();
			Definition d = new Definitions().GetByID( this.DefinitionID );
			if( d == null ) GoBack();
			Process p = new Processes().GetProcessByID( this.ProcessID );
			if( p == null ) GoBack();
			ProcessStart ps = new ProcessStarts().GetProcessStartByID( this.ProcessStartID );
			if( ps == null ) GoBack();
			
			// Display infos
			labHandler.Text = h.Name;
			labDefinition.Text = d.DefName;
			labProcess.Text = p.ProcessName;
			labMethod.Text = ps.Method;
		}
		
		// Load the process start record
		ProcessStart procStart = new ProcessStarts().GetProcessStartByID( this.ProcessStartID );
		if( procStart == null ) GoBack();
		
		// Create engine and get the config method
		WorkflowEngine engine = new WorkflowEngine( this.HandlerID );
		ConfigClass = engine.GetConfigObjectForMethod( procStart.Method );
		if( ConfigClass == null ) GoBack();

		// Clear dynamic form
		formhost.Controls.Clear();
		// Add all the controls as specified
		foreach( Control ctrl in ConfigClass.AddFormControls( this.Page, HandlerID, DefinitionID, ProcessID ))
			formhost.Controls.Add( ctrl );
	
		// If we have something to edit, load it up when not posting back
		if (!Page.IsPostBack)
		{
			ConfigClass.DeSerialise(procStart.PropertyBag);
			// Bind the data dictionary
			Dictionary.DataSource = engine.GetDataDictionary();
			Dictionary.DataBind();
		}
	}
	#endregion
	#region Navigation
	/// <summary>
	/// Returns to the listing, restoring state
	/// </summary>
	protected void GoBack()
	{
		string url = String.Format(
			"ui_processform.aspx?HandlerID={0}&DefinitionID={1}&ProcessID={2}",
			this.HandlerID,
			this.DefinitionID,
			this.ProcessID);

		Response.Redirect(url, true);
	}
	#endregion
	#region Save/Cancel handlers
	/// <summary>
	/// User clicked the back to process list button
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnBack_Click(object sender, EventArgs e)
	{
		GoBack();
	}
	/// <summary>
	/// User clicked the save button, update the data
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnSave_Click(object sender, EventArgs e)
	{
		if (!Page.IsValid) return;

		ProcessStarts pss = new ProcessStarts();
		ProcessStart ps = pss.GetProcessStartByID( this.ProcessStartID );
		if( ps == null ) GoBack();
		ps.PropertyBag = ConfigClass.Serialise();
		pss.Save( ps );
		
		QuickAudit.RaiseInformation(
			new AuditUser(),
			"Flux", "Workflow Manager",
			"modified properties of startup action ({0}) in process ({1})",
			ps.Method,
			ps.ParentProcess.ProcessName);

		GoBack();
	}
	#endregion
}
